//
//  CouponListNewViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/9/29.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//

#import "BusinessCombinationListViewController.h"
#import "BusinessCombinationAddViewController.h"

#import "HAODEsingleton.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "JSONKit.h"
#import "addhuiyuanViewController.h"
#import "CombinationListCell.h"
#import "RDVTabBarController.h"
#import "MJRefresh.h"
#import "UIButton+WebCache.h"
#import "huiyuanDetailsViewController.h"
#import "GetProductListViewController.h"
#import "TheLoginViewController.h"
#import "shouyingViewController.h"
//#import "BusinessCombinationDetailViewController.h"
#import "CombinatioanBusinessDetailViewController.h"
@interface BusinessCombinationListViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UISearchBarDelegate>
@property (strong, nonatomic) NSString *areaValue;
@property (strong, nonatomic) NSString*cityValue;


@end

@implementation BusinessCombinationListViewController

{
    UISearchBar *search;
    HAODEsingleton* commdity;
    MJRefreshHeaderView* _header;
    MJRefreshFooterView* _footer;
    NSMutableArray*cbId;
    NSMutableArray*title;
    NSMutableArray*detail;
    NSMutableArray*qty;
    NSMutableArray*imageList;
    NSMutableArray*endDate;
    NSMutableArray*imageUrl;
    NSMutableArray *typeCodeArray;
     NSMutableArray *typeNameArray;
   
    UILabel* noLabel;
    NSMutableArray*imageUrlFull;
    NSString *result;
    int pageNumber;
    MBProgressHUD *progressHud;
    
}
@synthesize alt = alt;

-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    
        self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    self.title=@"组合商品";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    
    
    if (  [commdity.gengxin isEqualToString:@""]) {
         [self add];
    }else{
    
    
    
    }
   
    
}


- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
    [self addFooter];
    _table.delegate=self;
    _table.dataSource=self;
    [self.view addSubview:_table];
    _table.separatorStyle=UITableViewCellSeparatorStyleNone;
    commdity=[HAODEsingleton shared];
    commdity.content3=@"";
    UIButton*tianjiasxBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0,  60/2, 60/2)];
    [tianjiasxBtn setImage:[UIImage imageNamed:@"tianjia"] forState:UIControlStateNormal];
    [tianjiasxBtn addTarget:self action:@selector(addProduct:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:tianjiasxBtn];
    self.navigationItem.rightBarButtonItem = myBtnBtn;
    
}

-(void)addProduct:(UIButton*)sender{
    commdity.gengxin=@"youshuju";
    commdity.content=nil;
    commdity.caseStylecontent=nil;
    commdity.caseHousecontent=nil;
    
    commdity.code=nil;
    commdity.caseHouseCode=nil;
    commdity.caseStyleCode=nil;
    commdity.imageUrlFullarr=nil;
    
    BusinessCombinationAddViewController *detailViewController = [[BusinessCombinationAddViewController alloc] init];
    detailViewController.editOrAddString=@"Add";
    [self.navigationController pushViewController:detailViewController animated:YES];
    
    
}


-(void)add{
    
    [noLabel removeFromSuperview];
    
    self.sat=nil;
    
    
    pageNumber = 1;
    
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token ==nil) {
        Token=@"dd";
    }
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
  
    [param setValue: @"20" forKey:@"pageSize"];
    [param setValue: @"1" forKey:@"pageIndex"];
    
  
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:20 view:self.view];
    
    
    [HttpRequest requestData:@"Business/Combination/List" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
      NSLog(@"11QQ====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                [severDataDict.dataDict writeToFile:@"/Users/liyindi/Desktop/CombinationList.plist" atomically:YES];
                NSString*TotalCount=[severDataDict.dataDict objectForKey:@"TotalCount"];
                commdity.Dict=severDataDict.dataDict;
                if (TotalCount.intValue==0) {
                    
                    noLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,ScreenHeight/2-50, ScreenWidth, 17)];
                    noLabel.text=@"无数据";
                    noLabel.textColor=[UIColor blackColor];
                    noLabel.textAlignment=NSTextAlignmentCenter;
                    
                    [self.view addSubview:noLabel];
                    
                }else{
                    
                    imageUrlFull= [[NSMutableArray alloc] init];
                    title= [[NSMutableArray alloc] init];
                    cbId= [[NSMutableArray alloc] init];
                    detail= [[NSMutableArray alloc] init];
                    imageList = [[NSMutableArray alloc] init];
                    typeCodeArray= [[NSMutableArray alloc] init];
                    typeNameArray= [[NSMutableArray alloc] init];
                    NSMutableArray* arrr=[[NSMutableArray alloc]init];
                    arrr=[severDataDict.dataDict objectForKey:@"DataLine"];
                    for (NSDictionary* dicc in arrr )
                    {
                        [title addObject:[dicc valueForKey:@"title"]];
                        [cbId addObject:[dicc valueForKey:@"cbId"]];
                        [detail addObject:[dicc valueForKey:@"detail"]];
                        [typeCodeArray addObject:[dicc valueForKey:@"typeCode"]];
//                        [imageUrlFull addObject:[dicc valueForKey:@"imageUrlFull"]];
                       [typeNameArray addObject:[dicc valueForKey:@"typeName"]];
                        [imageList addObject:[dicc valueForKey:@"imageList"]];
                    }
                    
                    [_table reloadData];
                    
                }
                
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    
                    //                   NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //                  [Global showTextHud:requestResultStr view:self.view];
                    
                    
                    TheLoginViewController *detailViewController = [[TheLoginViewController alloc] init];
                    [self.navigationController pushViewController:detailViewController animated:YES];
                }
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    
    
    
    
    
    
    
    
}





//上拉刷新
- (void)addFooter
{
    
    
    
    MJRefreshFooterView* footer = [MJRefreshFooterView footer];
    footer.scrollView = _table;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView* refreshView)
    {
        
        
        
        [self performSelector:@selector(doneWithView:) withObject:refreshView afterDelay:3.0f];
        NSLog(@"%@--->正在刷新", refreshView.class);
    };
    
}
- (void)doneWithView:(MJRefreshBaseView *)refreshView
{
    [_table reloadData];
    [refreshView endRefreshing];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [cbId count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    CombinationListCell* cell = (CombinationListCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"CombinationListCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    UIButton*jiantou = [[UIButton alloc]init];
    jiantou.frame = CGRectMake(0,0, 8, 13);
    [jiantou setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
    
    //cell.accessoryView = jiantou;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    
//    [cell.imageUrlFull.layer setBorderColor:colorref2];//边框颜色
//    [cell.imageUrlFull.layer setCornerRadius:0.5];
//    [cell.imageUrlFull.layer setBorderWidth:0.1];
//    [cell.imageUrlFull.layer setMasksToBounds:YES];
//    CGColorSpaceRef colorSpace21 = CGColorSpaceCreateDeviceRGB();
//    CGColorRef colorref21 = CGColorCreate(colorSpace21,(CGFloat[]){ 0, 0, 0, 0.3 });
//    [cell.imageUrlFull.layer setBorderColor:colorref21];//边框颜色
    [cell.imageUrlFull setImageWithURL:[NSURL URLWithString:[[imageList objectAtIndex:indexPath.row][0]objectForKey:@"imageUrlFull"]] placeholderImage:[UIImage imageNamed:@""]];
    [cell.imageUrlFull setContentMode:UIViewContentModeScaleToFill];
    
    
    cell.title.text =[NSString stringWithFormat:@"%@",  [title objectAtIndex:indexPath.row]];
    cell.title.font=[UIFont systemFontOfSize:15];
    cell.title.textColor=[UIColor blackColor];
    
    
    
    
    cell.price.text =[NSString stringWithFormat:@"%@",  [detail objectAtIndex:indexPath.row]];
    
    
    
    if (detail.count !=indexPath.row+1) {
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(15, 300-0.35, ScreenWidth-30, 0.35)];
        lineLabel1.backgroundColor=[UIColor groupTableViewBackgroundColor];;
        [cell.contentView addSubview:lineLabel1];
        
    }
    
    if (detail.count ==indexPath.row+1) {
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(0, 300-0.35, ScreenWidth, 0.35)];
        lineLabel1.backgroundColor=[UIColor groupTableViewBackgroundColor];
        [cell.contentView addSubview:lineLabel1];
        
    }

    
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return  300;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    commdity.actTypeCode=cbId[indexPath.row];
    commdity.gengxinxiangqing=@"";
    commdity.gengxin=@"123";

    CombinatioanBusinessDetailViewController *vc1=[[CombinatioanBusinessDetailViewController alloc] init];
     vc1.cbIdStr=cbId[indexPath.row];
    vc1.titleStr=title[indexPath.row];
    vc1.detailStr=detail[indexPath.row];
    vc1.imageListArray=imageList[indexPath.row];
    vc1.itemTypeCodeStr=[typeCodeArray objectAtIndex:indexPath.row];
   vc1.itemTypeNameStr=[typeNameArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:vc1 animated:YES];
  

}




- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}


@end
